<?php
/*
	JWT口令验证
*/
namespace phpjwt;
use \Firebase\JWT\JWT;
require 'src/JWT.php';
class JwtAuth
{
	private static $instance=null;
	private $salt='xianjidan-adsdaxx';
	private function __construct()
	{
		# code...
	}
	private function __clone()
	{
		# code...
	}
	static public function getInstance()
	{
		if (!self::$instance instanceof self) {
            self::$instance = new self();
        }
        return self::$instance;
	}
	#加密
	public function encode($data=[])
	{
		if (!is_array($data)) {
			echo '加密数据必须是数组';
			return '';
		}
		$key = $this->salt;
		$token = array(
		    "iss" => "http://example.org",
		    "aud" => "http://example.com",
		    "iat" => 1356999524,
		    "nbf" => 1357000000
		);
		foreach ($data as $value) {
			array_push($token, $value);
		}
		$jwt = JWT::encode($token, $key);
		return (string)$jwt;
	}
	#解密
	public function decode($token)
	{
		$decoded = JWT::decode($jwt, $key, array('HS256'));
		return (array)$decoded;
	}
}